#!/bin/bash

cwd=`pwd`
init_sys () {
echo "initial The system parameters"
echo "ulimit SHn 65535" >>/etc/rc.local
cat >>/etc/security/limits.conf<<EOF
*    soft    nofile    65535
*    hard    nofile    65535
*    soft    nproc    65535
*    hard    nproc    65535
EOF
cat >>/etc/sysctl.conf<<EOF
net.ipv4.tcp_max_tw_buckets = 10000
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 262144
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_probes = 4
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024  65535
net.ipv4.tcp_max_syn_backlog = 8192
EOF
sysctl -p
sed -i 's/SELINUX=enforcing/SELINUX=disable/' /etc/sysconfig/selinux
for i in {iptables,ip6tables,cups};do chkconfig $i off;service $i stop;done
echo "/usr/sbin/setenforce 0" >> /etc/rc.local
}

sys_update () {
    cd $cwd
    rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    yum update -y
    yum install -y lua-devel expat-devel libtool lrzsz perl-ExtUtils-Embed gcc gcc-c++ cmake openssl openssl-devel libevent libevent-devel libxml2 libxml2-devel libaio libaio-devel gd gd-devel mcrypt libmcrypt libmcrypt-devel curl curl-devel pcre pcre-devel freetype freetype-devel libjpeg libjpeg-devel libpng libpng-devel ncurses ncurses-devel bison bison-devel
}

install_mysql () {
    cd $cwd/src
    groupadd mysql
    useradd -g mysql mysql
    wget http://mirrors.opencas.cn/mariadb/mariadb-10.0.17/source/mariadb-10.0.17.tar.gz
    tar xvf mariadb-10.0.17.tar.gz
    cd mariadb-10.0.17
    cmake . \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DMYSQL_DATADIR=/data/mysql \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DEXTRA_CHARSETS=all \
    -DENABLED_LOCAL_INFILE=1
    res=$?
    if [ $res == 0 ];then make -j4 && make install;else exit 2;fi
    ln -s /usr/local/mysql/lib/libmysqlclient.so.18.0.0 /usr/lib64/libmysqlclient.so.18
    /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
    rm -f /etc/my.cnf
    cp $cwd/conf/my.cnf /usr/local/mysql/my.cnf
    cp $cwd/scripts/mysqld /etc/init.d/ && chmod 755 /etc/init.d/mysqld
}

install_tenginx () {
    useradd www -s /sbin/nologin
    cd $cwd/src
    tar xvf lua-nginx-module-0.9.13rc1.tar.gz
    tar xvf ngx_devel_kit-0.2.19.tar.gz
    wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
    tar xvf tengine-2.1.0.tar.gz
    cd tengine-2.1.0
    ./configure \
    --prefix=/usr/local/tengine \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_image_filter_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_concat_module \
    --with-pcre \
    --with-http_spdy_module \
    --add-module=$cwd/src/ngx_devel_kit-0.2.19 \
    --add-module=$cwd/src/lua-nginx-module-0.9.13rc1
    res=$?
    if [ $res == 0 ];then make -j4 && make install;else exit 2;fi
    cp $cwd/scripts/nginx /etc/init.d/ && chmod 755 /etc/init.d/nginx
}

install_memcached () {
    cd $cwd/src
    wget http://www.memcached.org/files/memcached-1.4.22.tar.gz
    tar xvf memcached-1.4.22.tar.gz
    cd memcached-1.4.22
    ./configure --prefix=/usr/local/memcached
    res=$?
    if [ $res == 0 ];then make -j4 && make install;else exit 2;fi
    cp $cwd/scripts/memcached /etc/init.d/ && chmod 755 /etc/init.d/memcached
}

install_php () {
    cd $cwd/src
    wget http://cn2.php.net/distributions/php-5.3.29.tar.bz2
    tar xvf php-5.3.29.tar.bz2
    cd php-5.3.29
    ./configure \
    --prefix=/usr/local/php \
    --with-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-zlib-dir \
    --with-gd \
    --with-libxml-dir \
    --with-freetype-dir \
    --with-jpeg-dir \
    --with-png-dir \
    --with-mhash \
    --with-mcrypt \
    --with-iconv \
    --with-openssl \
    --with-gettext \
    --enable-bcmath \
    --enable-shmop \
    --enable-fpm \
    --enable-mbstring \
    --enable-gd-native-ttf \
    --enable-gd-jis-conv \
    --enable-static \
    --enable-sockets \
    --enable-soap \
    --enable-zip \
    --enable-session \
    --with-curl \
    --disable-ipv6 \
    --with-pdo-mysql=mysqlnd
    res=$?
    if [ $res == 0 ];then make -j4 && make install;else exit 2;fi
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod 755 /etc/init.d/php-fpm
    cp $cwd/conf/php-fpm.conf /usr/local/php/etc/php-fpm.conf
    cp $cwd/conf/php.ini /usr/local/php/lib/php.ini
}

install_memcache () {
    cd $cwd/src
    wget http://pecl.php.net/get/memcache-2.2.7.tgz
    tar xvf memcache-2.2.7.tgz
    cd memcache-2.2.7
    /usr/local/php/bin/phpize && ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
    make && make install    
}

install_zendopcache () {
    cd $cwd/src
    wget http://pecl.php.net/get/zendopcache-7.0.5.tgz
    tar xvf zendopcache-7.0.5.tgz
    cd zendopcache-7.0.5
    /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config
    make && make install    
}

install_phpredis () {
    cd $cwd/src
    wget http://pecl.php.net/get/redis-2.2.5.tgz
    tar xvf redis-2.2.5.tgz
    cd redis-2.2.5
    /usr/local/php/bin/pecl install igbinary
    /usr/local/php/bin/phpize && ./configure --enable-redis --enable-redis-igbinary --with-php-config=/usr/local/php/bin/php-config
    make && make install
}

install_redis () {
    cd $cwd/src
    wget http://download.redis.io/releases/redis-3.0.0.tar.gz
    tar xvf redis-3.0.0.tar.gz
    cd redis-3.0.0
    make -j4 && make install
    mkdir /etc/redis && cp $cwd/conf/redis.conf /etc/redis/
    echo "/usr/local/bin/redis-server /etc/redis/redis.conf" >> /etc/rc.local
}

#init_sys
#sys_update
#install_mysql
#install_tenginx
#install_memcached
#install_php
#install_memcache
#install_zendopcache
#install_phpredis
#install_redis
